package io.renren.modules.project.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.DateUtils;
import io.renren.modules.project.controller.ChartQualityController;
import io.renren.modules.project.dao.AuthachVoDao;
import io.renren.modules.project.dao.ProjectAuthachDao;
import io.renren.modules.project.entity.ProjectAuthachEntity;
import io.renren.modules.project.service.ProjectAuthachService;
import io.renren.modules.project.vo.AuthachVoEntity;
import io.renren.modules.project.vo.ChartOutputVoEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Map;
import java.util.List;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;


@Service("projectAuthachService")
public class ProjectAuthachServiceImpl extends ServiceImpl<ProjectAuthachDao, ProjectAuthachEntity> implements ProjectAuthachService {

    @Autowired
    private AuthachVoDao authachVoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = (String)params.get("key");

        Page<ProjectAuthachEntity> page = this.selectPage(
                new Query<ProjectAuthachEntity>(params).getPage(),
                new EntityWrapper<ProjectAuthachEntity>().like(StringUtils.isNotBlank(key),"id", key)
        );

        return new PageUtils(page);
    }

    @Override
    public List<ProjectAuthachEntity> queryList(Map<String, Object> params){
        List<ProjectAuthachEntity> list = this.selectList(
                new EntityWrapper<ProjectAuthachEntity>()
        );
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ProjectAuthachEntity queryByPro(String projectNo){
        ProjectAuthachEntity entity = this.selectOne(new EntityWrapper<ProjectAuthachEntity>().eq("project_no",projectNo));

        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ProjectAuthachEntity entity) {
        ProjectAuthachEntity authachEntity = this.selectOne(
                new EntityWrapper<ProjectAuthachEntity>().eq("project_no",entity.getProjectNo())
        );
        if (authachEntity != null) {
            entity.setId(authachEntity.getId());
        }
        this.insertOrUpdate(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ProjectAuthachEntity entity) {
        this.updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] Ids) {
        //删除
        this.deleteBatchIds(Arrays.asList(Ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<AuthachVoEntity> getAuthVoList(Map<String, Object> params){
        return authachVoDao.getAuthachVoList(params);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public HSSFWorkbook exportExcel(Map<String, Object> params) {
        List<AuthachVoEntity> list = authachVoDao.getAuthachVoList(params);

        String title = "";
        if (StringUtils.isNotBlank(params.get("startDate").toString())) {
            title += params.get("startDate").toString();
        }
        if (StringUtils.isNotBlank(params.get("endDate").toString())) {
            title += params.get("endDate").toString();
        } else {
            title += "至今";
        }
        title += "  满意度调查统计表";
        String[] titleArr = new String[]{"项目编号","项目名称","项目负责人","项目启动时间","成果质量","要求质量","工作态度","进度情况","熟练程度","评分总分"};
        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        int rowIndex = 0;
        //生成一个表格，设置表格名称
        HSSFSheet sheet = workbook.createSheet("sheet1");
        //设置表格列宽度
        for (int i=0; i< titleArr.length; i++) {
            sheet.setColumnWidth(i, i==1? (180 * 100) : (40*100));
        }
        // 标题字体设置
        HSSFFont hfont = workbook.createFont();
        hfont.setFontName("黑体");
        hfont.setFontHeightInPoints((short) 16);
        hfont.setBold(true);   //字体加粗
        // 标题表格样式
        HSSFCellStyle hstyle = workbook.createCellStyle();
        hstyle.setFont(hfont);
        hstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  // 垂直居中
        hstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);       // 水平居中


        // 标题行
        HSSFRow hRow = sheet.createRow(rowIndex++);
        hRow.setHeight((short) 540);
        HSSFCell hcell = hRow.createCell(0);
        hcell.setCellValue(new HSSFRichTextString(title));
        hcell.setCellStyle(hstyle);
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 9));

        // 字段字体设置
        HSSFFont tfont = workbook.createFont();
        tfont.setFontName("宋体");
        tfont.setFontHeightInPoints((short) 12);
        tfont.setBold(true);   //字体加粗
        // 字段表格样式
        HSSFCellStyle tstyle = workbook.createCellStyle();
        tstyle.setFont(tfont);
        tstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);  // 垂直居中
        // 红色字体
        HSSFFont rfont = workbook.createFont();
        rfont.setColor(Font.COLOR_RED);
        HSSFCellStyle rstyle = workbook.createCellStyle();
        rstyle.setFont(rfont);

        // 字段行
        HSSFRow tRow = sheet.createRow(rowIndex++);
        tRow.setHeight((short) 410);
        HSSFCell tcell;
        for (int i=0; i< titleArr.length; i++) {
            tcell = tRow.createCell(i);
            tcell.setCellValue(new HSSFRichTextString(titleArr[i]));
            tcell.setCellStyle(tstyle);
        }

        for(AuthachVoEntity item : list) {
            // 各队明细产值
            // 工作组名字体设置
            HSSFFont gfont = workbook.createFont();
            gfont.setFontName("宋体");
            gfont.setFontHeightInPoints((short) 11);
            HSSFCellStyle gstyle = workbook.createCellStyle();
            gstyle.setFont(gfont);
            HSSFRow gRow = sheet.createRow(rowIndex++);
            HSSFCell gcell = gRow.createCell(0);
            gcell.setCellValue(new HSSFRichTextString(item.getProjectNo()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(1);
            gcell.setCellValue(new HSSFRichTextString(item.getProjectName()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(2);
            gcell.setCellValue(new HSSFRichTextString(item.getProjectCharge()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(3);
            gcell.setCellValue(new HSSFRichTextString(DateUtils.format(item.getProjectStartDateTime())));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(4);
            gcell.setCellValue(new HSSFRichTextString(item.getGradeQuality().toString()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(5);
            gcell.setCellValue(new HSSFRichTextString(item.getGradeRequire().toString()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(6);
            gcell.setCellValue(new HSSFRichTextString(item.getGradeAttitude().toString()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(7);
            gcell.setCellValue(new HSSFRichTextString(item.getGradeProgress().toString()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(8);
            gcell.setCellValue(new HSSFRichTextString(item.getGradeProficiency().toString()));
            gcell.setCellStyle(gstyle);
            gcell = gRow.createCell(9);
            gcell.setCellValue(new HSSFRichTextString(this.getScoreSum(item)));
            gcell.setCellStyle(rstyle);
        }
        return workbook;
    }

    /**
     * 总分数
     * @return
     */
    public String getScoreSum(AuthachVoEntity item) {
        Long sum = item.getGradeQuality() +
            item.getGradeRequire() +
            item.getGradeAttitude() +
            item.getGradeProgress() +
            item.getGradeProficiency();
        return String.valueOf(sum);
    }
}